package com.whenjun.service.impl;

import com.ifootbook.util.*;
import com.whenjun.dto.base.BaseId;
import com.whenjun.dto.kq.KqGroupAddReq;
import com.whenjun.dto.kq.KqDepartmentGroupDTO;
import com.whenjun.dto.kq.KqGroupReq;
import com.whenjun.dto.kq.KqGroupUpdReq;
import com.whenjun.entity.KqDepartment;
import com.whenjun.entity.KqDepartmentGroup;
import com.whenjun.mapper.KqDepartmentGroupMapper;
import com.whenjun.service.IKqDepartmentGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-22
 */
@Service
public class KqDepartmentGroupServiceImpl extends ServiceImpl<KqDepartmentGroupMapper, KqDepartmentGroup> implements IKqDepartmentGroupService {

//    @Autowired
//    private KqDepartmentServiceImpl kqDepartmentService;

    @Autowired
    private KqDepartmentGroupPersonServiceImpl kqDepartmentGroupPersonService;

    @Transactional
    public List<KqDepartmentGroupDTO> KqGroups(KqGroupReq kqGroupReq) {
        List<KqDepartmentGroup> kqGroups = this.lambdaQuery()
                .eq(KqDepartmentGroup::getDepartmentId, kqGroupReq.getId())
                .eq(KqDepartmentGroup::getState, false)
                .eq((kqGroupReq != null && !EmptyUtil.isEmpty(kqGroupReq.getName())), KqDepartmentGroup::getName, kqGroupReq.getName())
                .orderByDesc(KqDepartmentGroup::getCreateTime)
                .list();

        List<KqDepartmentGroupDTO> kqGroupDTOS = CopyerObject.copyerListObject(kqGroups, KqDepartmentGroupDTO.class);
        return kqGroupDTOS;
    }

    @Transactional
    public void KqGroupAdd(KqGroupAddReq kqGroupAddReq) {
        // 部门id 校验
//        KqDepartment kqDepartment = kqDepartmentService.getById(kqGroupAddReq.getId());
//        if (kqDepartment == null || kqDepartment.getState().equals(true)){
//            throw new FootBookException("id错误");
//        }

        // name 唯一性校验
        KqDepartmentGroup kqDepartmentGroup = this.lambdaQuery()
                .eq(KqDepartmentGroup::getName, kqGroupAddReq.getName())
                .eq(KqDepartmentGroup::getState, false)
                .one()
                ;

        // 上下班时间校验
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateStyle.HH_MM_SS.getValue());
        try {
            simpleDateFormat.parse(kqGroupAddReq.getShangbanTime());
            simpleDateFormat.parse(kqGroupAddReq.getXiabanTime());
        }catch (Exception e){
            throw new FootBookException("时间格式错误，请校对");
        }

        if (kqDepartmentGroup != null) {
            throw new FootBookException("name已存在，请修改后再试");
        }

        String userId = SaTokenUtil.getUserId();
        KqDepartmentGroup kqGroup = CopyerObject.copyerObject(kqGroupAddReq, KqDepartmentGroup.class);
        LocalDateTime now = LocalDateTime.now();
        kqGroup.setId(Util.getUUID32());
        kqGroup.setDepartmentId(kqGroupAddReq.getId());
        kqGroup.setCreateUser(userId);
        kqGroup.setState(false);
        kqGroup.setCreateTime(now);

        kqGroup.setShangbanTime(kqGroupAddReq.getShangbanTime());
        kqGroup.setXiabanTime(kqGroupAddReq.getXiabanTime());
        this.save(kqGroup);
    }

    @Transactional
    public void KqGroupDel(BaseId baseId) {
        KqDepartmentGroup kqGroup = this.getById(baseId.getId());
        if (kqGroup == null) {
            throw new FootBookException("id错误");
        }
        this.removeById(kqGroup);
        // 删除group所属人员
        kqDepartmentGroupPersonService.KqGroupPersonsRmByGroupId(kqGroup.getId());
    }

    @Transactional
    public void KqGroupDelByDepartmentId(String departmentId) {
        List<KqDepartmentGroup> kqGroups = this.lambdaQuery()
                .eq(KqDepartmentGroup::getDepartmentId, departmentId)
                .list();

        for (KqDepartmentGroup kqGroup : kqGroups) {
            this.removeById(kqGroup);
            // 删除group所属人员
            kqDepartmentGroupPersonService.KqGroupPersonsRmByGroupId(kqGroup.getId());
        }
    }

    @Transactional
    public void KqGroupUpd(KqGroupUpdReq kqGroupUpdReq) {
        KqDepartmentGroup kqGroup = this.getById(kqGroupUpdReq.getId());
        if (kqGroup == null || kqGroup.getState().equals(true)) {
            throw new FootBookException("id错误");
        }
        // 上下班时间校验
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateStyle.HH_MM_SS.getValue());
        try {
            simpleDateFormat.parse(kqGroupUpdReq.getShangbanTime());
            simpleDateFormat.parse(kqGroupUpdReq.getXiabanTime());
        }catch (Exception e){
            throw new FootBookException("时间格式错误，请校对");
        }
        // name 唯一性校验
        KqDepartmentGroup kqDepartmentGroup = this.lambdaQuery()
                .eq(KqDepartmentGroup::getName, kqGroupUpdReq.getName())
                .eq(KqDepartmentGroup::getState, false)
                .one()
                ;
        if (kqDepartmentGroup != null && !kqDepartmentGroup.getName().equals(kqGroup.getName())) {
            throw new FootBookException("name已存在，请修改后再试");
        }

        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();
        kqGroup.setName(kqGroupUpdReq.getName());
        kqGroup.setUpdateTime(now);
        kqGroup.setUpdateUser(userId);

        kqGroup.setShangbanTime(kqGroupUpdReq.getShangbanTime());
        kqGroup.setXiabanTime(kqGroupUpdReq.getXiabanTime());
        this.updateById(kqGroup);
    }
}
